package edu.gatech.cs2340.OregonTrail;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import edu.gatech.cs2340.OregonTrail.EventSystem.ActorEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.CommCoordinator;
import edu.gatech.cs2340.OregonTrail.EventSystem.FoodEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.IllnessEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.OxEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.WagonEvent;

public class RandomEventGenerator {
	private List<ActorEvent> eventList;
	private GameDB gameDB;
	private double breakChance;
	private double sickChance;
	private Random randGenerator;
	private CommCoordinator comm;
	
	public RandomEventGenerator(CommCoordinator comm, double d, double e){
		this.gameDB = GameDB.getGameDB();
		this.eventList = gameDB.getEventList();
		this.breakChance = d;
		this.sickChance = e;
		this.randGenerator = new Random();
		this.comm = comm;
	}
	
	public void generateRandomEvent(){
		int chance = randGenerator.nextInt(100);
		if (chance <= 30){
			int baseChance = 4;
			if(randGenerator.nextInt(100) <= (sickChance * 100)){
				baseChance += 1;
			}
			
			if(randGenerator.nextInt(100) <= (breakChance * 100)){
				baseChance += 1;
			}
			
			int event = randGenerator.nextInt(baseChance);
			
			switch (event){
				case 0:
					generateFoodEvent();
					break;
				case 1:
					generateIllnessEvent();
					break;
				case 2:
					generateOxEvent();
					break;
				
				case 3:
					generateWagonEvent();
					break;
				
				default:
					break;
			}
		}
	}
	
	public void generateWagonEvent() {
		List<WagonEvent> wagonEvents = new ArrayList<WagonEvent>();
		
		for(int i = 0; i < eventList.size(); i++){
			if(eventList.get(i).getClass() == WagonEvent.class){
				wagonEvents.add((WagonEvent) eventList.get(i));
			}
		}
		
		int event = randGenerator.nextInt(wagonEvents.size());
		
		comm.ThrowActorEvent(wagonEvents.get(event));		
	}

	public void generateOxEvent() {
		List<OxEvent> oxEvents = new ArrayList<OxEvent>();
		
		for(int i = 0; i < eventList.size(); i++){
			if(eventList.get(i).getClass() == OxEvent.class){
				oxEvents.add((OxEvent) eventList.get(i));
			}
		}
		
		int event = randGenerator.nextInt(oxEvents.size());
		
		comm.ThrowActorEvent(oxEvents.get(event));		
	}

	public void generateIllnessEvent() {
		List<IllnessEvent> illEvents = new ArrayList<IllnessEvent>();
		
		for(int i = 0; i < eventList.size(); i++){
			if(eventList.get(i).getClass() == IllnessEvent.class){
				illEvents.add((IllnessEvent) eventList.get(i));
			}
		}
		
		int event = randGenerator.nextInt(illEvents.size());
		
		comm.ThrowActorEvent(illEvents.get(event));		
	}

	public void generateFoodEvent(){
		List<FoodEvent> foodEvents = new ArrayList<FoodEvent>();
		
		for(int i = 0; i < eventList.size(); i++){
			if(eventList.get(i).getClass() == FoodEvent.class){
				foodEvents.add((FoodEvent) eventList.get(i));
			}
		}
		
		int event = randGenerator.nextInt(foodEvents.size());
		
		comm.ThrowActorEvent(foodEvents.get(event));		
	}
}
